<?php
/* ***************************************
 * Copyright @ chinaaus              **
 * @Procedure: class of DB operation **
 * @Designer: Cavin                 **
 * @Date: 17 July, 2011           **
 * *****************************/

class CsDatabaseOpera{
	var $dbLink; 
	var $queryId;
	var $dbHostName;
	var $dbUsrName;
	var $dbPsWd;
	var $dbName;
	var $queryResult;
	var $rsRows;  // return the number of the query result
	
	function CsDatabaseOpera($hostName, $usrName, $psWord,$dbName)
	{	
		$this -> dbHostName = $hostName;
		$this -> dbUsrName = $usrName;
		$this -> dbPsWd = $psWord;
		$this -> dbName = $dbName;
		$this -> dbLink = mysql_connect($this -> dbHostName, $this -> dbUsrName, $this -> dbPsWd);
		mysql_select_db($this -> dbName);
		mysql_query("SET NAMES 'gbk'",$this -> dbLink);
	}
	
	/*
	 * execute basical Query 
	 */
	function executeBasicQuery($sql)
	{
		if($this -> dbLink == "")
		{
			$this -> CsDatabaseOpera($this->dbHostName, $this->dbUsrName, $this->dbPsWd, $this->dbName);
		}
		//echo $sql;
		$this -> queryResult = mysql_query($sql);
		
		return $this -> queryResult;
	}
	
	/*
	 * just get only one row of the queried results 
	 * return an array
	 */
	function getOneQuery($sql)
	{
		$row = '';
		$this -> executeBasicQuery($sql);
		if($this -> getQueryNum($sql) > 0)
		{
			$row = mysql_fetch_array($this -> queryResult);
			return $row;
		}
		else
		{
			return FALSE;
		}
	}
	
	/*
	 * return an array after send the query
	 */
	function executeQuery($sql)
	{
		//$this -> executeBasicQuery($sql);
		if($this -> getQueryNum($sql) > 0)
		{
			while($row = mysql_fetch_array($this -> queryResult))
			{
				$arr[] = $row;
			}
			return $arr;
		}
		else
		{
			return FALSE;
		}
	}
	
	/*
	 * get total nubmer of the querying result
	 */
	function getQueryNum($sql)
	{

		$this -> queryResult = $this -> executeBasicQuery($sql);

		$this -> rsRows = @mysql_num_rows($this -> queryResult);
		return $this -> rsRows;
	}
	
	/* Phrase of SQL "Insert" function */
	function executeInsert($arr, $tbName)
	{
		$sqlInsert = 'INSERT INTO `'.$tbName.'`(';
		$tbColumn = '';
		$tbValMapping = '';
		foreach ($arr as $key => $val)
		{
			$tbColumn .= '`'.$key.'`,';
			$tbValMapping .= "'".$val."',";			
		}
		$tbColumn = substr($tbColumn, 0, strlen($tbColumn)-1);
		$tbValMapping = substr($tbValMapping, 0, strlen($tbValMapping)-1);
		/* combine the SQL */
		$sqlInsert .= $tbColumn.') VALUES('. $tbValMapping. ')';
		
		//return the latest id
		$rs = $this -> executeBasicQuery($sqlInsert);
		if(!$rs){
			$this -> msgErr($sqlInsert);
		}
		//echo mysql_insert_id();
		return mysql_insert_id();
	}
	
	/* Phrase of SQL "Update" function */
	function executeUpdate($arr, $tbName, $whereOrderBy = ' 1=1 ')
	{
		$sqlUpdate = 'UPDATE `'. $tbName. '` SET';
		foreach ($arr as $key => $val)
		{
			$sqlUpdate .= "`". $key ."` = '". $val ."',";
		}
		$sqlUpdate = substr($sqlUpdate, 0, strlen($sqlUpdate)-1);
		$sqlUpdate .= ' WHERE '. $whereOrderBy;
		//echo $sqlUpdate;
		return $this -> executeBasicQuery($sqlUpdate);
	}
	
	/* Show the error in case there is some mistake in your sql phrase */
	function msgErr($sql)
	{
		echo "<h1>There is a mistake during the execution !</h1></br>";
		echo "<hr>";
		echo "Enclosed please check your SQL phrase below:</br>";
		echo $sql;
		exit;
	}
}